import request from "../request";

//注册
export const userReg = async ({ username, password }) => {
  try {
    let res = await request.post("/users", { username, password });
    console.log("注册结果", res);
    return {
      message: "注册成功",
      type: "success",
    };
  } catch (error) {
    return {
      message: "账号已被占用",
      type: "error",
    };
  }
};

// 加载账号列表
export const userGet = async () => {
  let res = await request.get("/users");
  console.log(res);
  return res.data.results;
};

//登录
//解构赋值
export const userLogin = async ({ username, password }) => {
  try {
    let res = await request.post("/login", { username, password });
    console.log("登录结果", res);
    return {
      message: "登录成功",
      type: "success",
      userInfo: res.data, //登录成功后，需要把后端下发的用户信息提供给状态机
    };
  } catch (error) {
    console.log("登录失败", error);
    let message = error.response.data.code == 211 ? "账号不存在" : "密码错误";
    return {
      message,
      type: "error",
    };
  }
};
